package com.xinmiao.test8;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;

import com.xinmiao.test8.adapter.DataBean;
import com.xinmiao.test8.adapter.MyAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private YuanView mYuanView;
    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mYuanView = findViewById(R.id.yuan);

        mYuanView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                mYuanView.clearyuan(300);
            }
        });

        mRecyclerView = findViewById(R.id.recyclerview);
        GridLayoutManager layoutmanager = new GridLayoutManager(this, 3);
        layoutmanager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
                int count = 3;
                if (adapter != null) {
                    int itemViewType = adapter.getItemViewType(position);
                    if (itemViewType == 0) {
                        count = 1;
                    }

                }
                return count;
            }
        });
//        LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutmanager);
        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();


                if (params != null) {
                    int spanSize = params.getSpanSize();
                    int spanIndex = params.getSpanIndex();
                    outRect.top = 10;

                    if (spanSize == 1) {
                        outRect.left = 5;
                        outRect.right = 5;
                    }
                }

            }
        });
        List<DataBean> list = new ArrayList<>();
        setList(list);
        mRecyclerView.setAdapter(new MyAdapter(this, getLayoutInflater(), list));

        mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
                return false;
            }

            @Override
            public void onTouchEvent(RecyclerView rv, MotionEvent e) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });


    }

    private void setList(List<DataBean> list) {

        for (int i = 0; i < 20; i++) {
            DataBean dataBean = new DataBean();
            dataBean.setType(i % 4 == 0 ? 1 : 0);
            list.add(dataBean);
        }
    }

    public void onclick(View view) {
//        ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.
//                makeScaleUpAnimation(view, view.getWidth() / 2,
//                        view.getHeight() / 2,0, 0);
//        ImageView view1 = (ImageView) view;
//        // TODO 自动生成的方法存根
//        view1.buildDrawingCache(true);
//        view1.buildDrawingCache();
//        Bitmap bitmap = view1.getDrawingCache();
//        view1.setDrawingCacheEnabled(false);
//        ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.
//                makeThumbnailScaleUpAnimation(view,bitmap,0,0);


        ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.
                makeSceneTransitionAnimation(this,view,getString(R.string.image));



        Intent intent = new Intent(this, ListRecvclerActivity.class);
        ActivityCompat.startActivity(this, intent, activityOptionsCompat.toBundle());

    }
}
